<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>filmMobile</title>
</head>

<body>
    <div id="app">
        <form @submit.prevent="submitForm">
            <input v-model="user.username" placeholder="用户名">
            <p v-if="!validateUsername">用户名不合法</p>
            <input v-model="user.password" placeholder="密码" type="password">
            <p v-if="!validatePassword">密码不合法</p>
            <input v-model="user.email" placeholder="邮箱">
            <p v-if="!validateEmail">邮箱不合法</p>
            <input v-model="user.address" placeholder="地址">
            <p v-if="!validateAddress">地址不合法</p>
            <button type="submit">提交</button>
          </form>
      </div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
    new Vue({
        el: "#app",
        data() {
            return {
      user: {
        username: '',
        password: '',
        email: '',
        address: ''
      }
    };
  },
       
  computed: {
  validateUsername() {
    if (this.username.length >= 5) {
      return true; 
    } else {
      return false; 
    }
  },
  validatePassword() {
    if (this.password.length >= 8) {
      return true; 
    } else {
      return false; 
    }
  },
  validateEmail() {
    const emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if (emailPattern.test(this.email)) {
      return true; 
    } else {
      return false; 
    }
  },
  validateAddress() {
    
    if (this.address.length >= 10) {
      return true; 
    } else {
      return false; 
    }
  }
},
methods: {
  submitForm() {
    if (this.validateUsername() && this.validatePassword() && this.validateEmail() && this.validateAddress()) {
      console.log('表单已提交');
    } else {
      console.log('表单验证未通过');
    }
  }
}

    });
</script>

</html>
<style>
body {
      font-family: Arial, sans-serif;
      background-color: #f2f2f2;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }

    #app {
      background-color: #fff;
      padding: 20px;
      border-radius: 5px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }

    form {
      display: flex;
      flex-direction: column;
    }

    input {
      margin: 10px 0;
      padding: 10px;
      border: 1px solid #ccc;
      border-radius: 5px;
      font-size: 16px;
    }

    p {
      color: red;
      margin: 5px 0;
      font-size: 14px;
    }

    button {
      background-color: #007BFF;
      color: #fff;
      border: none;
      border-radius: 5px;
      padding: 10px;
      font-size: 18px;
      cursor: pointer;
    }

    button:hover {
      background-color: #0056b3;
    }
</style>
